package keeto.com.vn.bms.ui.fragment;

import keeto.com.vn.bms.config.BMSCore;
import keeto.com.vn.bms.model.User.EditAdminModel;
import keeto.com.vn.bms.network.IBMSProtocol;
import keeto.com.vn.bms.ui.activity.R;
import keeto.com.vn.bms.ui.activity.ReportDetailActivity;
import keeto.com.vn.bms.utils.BMSListener;
import keeto.com.vn.bms.utils.BMSSharedPreferences;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

public class ContactBMSFragment extends BaseFragment{
	private TextView nameCompany;
	private TextView addressCompany;
	private TextView extraInfo;
	private TextView phone;
	private TextView email;
	private TextView website;
	private TextView skype;
	private TextView viber;
	private TextView kakao;
	private Context ctx;
	private EditAdminModel adminModel= new EditAdminModel();
	private BMSSharedPreferences preferences;
	private Listener listener= new Listener();
	
	
	public static ContactBMSFragment newInstance(){
		ContactBMSFragment fragment = new ContactBMSFragment();
		Bundle bundle = new Bundle();
		fragment.setArguments(bundle);
		return fragment;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		preferences= new BMSSharedPreferences(getActivity());
		BMSCore.getInstance(getActivity()).taskGetSupport(preferences.getUserAuthKey(getActivity()), listener);
		View v = inflater.inflate(R.layout.contact_bms_fragment_layout,null);
		initComponent(v);

		
		return v;
	}
	public void initComponent(View container){
		nameCompany=(TextView)container.findViewById(R.id.edt_name_company);
		addressCompany=(TextView)container.findViewById(R.id.edt_address_company);
		extraInfo=(TextView)container.findViewById(R.id.edt_extra_info);
		phone=(TextView)container.findViewById(R.id.edt_user_phone);
		email =(TextView)container.findViewById(R.id.edt_user_email);
		website=(TextView)container.findViewById(R.id.edt_user_web);
		skype=(TextView)container.findViewById(R.id.edt_user_skype);
		viber=(TextView)container.findViewById(R.id.edt_user_viber);
		kakao=(TextView)container.findViewById(R.id.edt_user_kakao);
	}
	class Listener extends BMSListener{

		@Override
		public void finishGetSuport(final int returnCode,String resultMsg , final EditAdminModel admin) {
			// TODO Auto-generated method stub
			super.finishGetSuport(returnCode,resultMsg, admin);
			getActivity().runOnUiThread(new Runnable() {
				public void run() {
					if(returnCode==IBMSProtocol.STATUS_CODE_OK){ 
						adminModel= admin;
						String nameCom=adminModel.getNameCompany();
						nameCompany.setText(nameCom);
						addressCompany.setText(adminModel.getAddressCompany());
						extraInfo.setText(adminModel.getExtraInfo());
						phone.setText(adminModel.getPhone());
						website.setText(adminModel.getWeb());
						email.setText(adminModel.getEmail());
						skype.setText(adminModel.getSkype());
						viber.setText(adminModel.getViber());
						kakao.setText(adminModel.getKakao());
						
					}else if(returnCode==IBMSProtocol.STATUS_CODE_GENERIC_FAIL){
						Toast.makeText(getActivity(),getActivity().getResources().getString(R.string.error_perpose), Toast.LENGTH_SHORT).show();
					}else if(returnCode==IBMSProtocol.STATUS_CODE_GENERIC_FAIL_CONECTTION){
						Toast.makeText(getActivity(), getActivity().getResources().getString(R.string.error_network_connection), Toast.LENGTH_SHORT).show();
					}else if(returnCode==IBMSProtocol.STATUS_CODE_EXIT_RECORD){
						Toast.makeText(getActivity(), getActivity().getResources().getString(R.string.error_data_server), Toast.LENGTH_SHORT).show();
					} 
				}
			});
		}
		
		
	}
}
